
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.crawldb;


import com.tompai.datagather.conf.DefaultConfigured;
import com.tompai.datagather.model.CrawlDatum;
import com.tompai.datagather.model.CrawlDatums;
import com.tompai.datagather.util.ConfigurationUtils;

/**
* @author: tompai
* @createTime: 2018年4月28日 下午7:42:41
* @history:
* @version: v1.0
*/
public abstract class DBManager extends DefaultConfigured implements Injector, SegmentWriter{

    public abstract boolean isDBExists();

    public abstract void clear() throws Exception;

    public abstract Generator createGenerator();

    public Generator createGenerator(GeneratorFilter generatorFilter){
        Generator generator = createGenerator();
        generator.setFilter(generatorFilter);
        generator.setTopN(getConf().getTopN());
        ConfigurationUtils.setTo(this, generator, generatorFilter);
        return generator;
    }

    public abstract void open() throws Exception;

    public abstract void close() throws Exception;

    public abstract void inject(CrawlDatum datum, boolean force) throws Exception;

    public abstract void inject(CrawlDatums datums, boolean force) throws Exception;

    public abstract void merge() throws Exception;

    public void inject(CrawlDatum datum) throws Exception {
        inject(datum, false);
    }

//    public void inject(CrawlDatums datums, boolean force) throws Exception {
//        for (CrawlDatum datum : datums) {
//            inject(datum, force);
//        }
//    }

    public void inject(CrawlDatums datums) throws Exception {
        inject(datums, false);
    }

    public void inject(Iterable<String> links, boolean force) throws Exception {
        CrawlDatums datums = new CrawlDatums(links);
        inject(datums, force);
    }

    public void inject(Iterable<String> links) throws Exception {
        inject(links, false);
    }

    public void inject(String url, boolean force) throws Exception {
        CrawlDatum datum = new CrawlDatum(url);
        inject(datum, force);
    }

    public void inject(String url) throws Exception {
        CrawlDatum datum = new CrawlDatum(url);
        inject(datum);
    }

}
